<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-student-dormitory-add">
			<div class="form-group">
				<label class="col-sm-3 control-label">宿舍号：</label>
				<div class="col-sm-8">
					<input id="number" name="number" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">宿舍类型：</label>
				<div class="col-sm-8" th:with="type=${@dict.getType('module_student_dormitory_type')}">
					<div th:each="dict : ${type}" th:class="${dict['cssClass']}">
						<input type="radio" th:id="${dict['dictCode']}" name="type" th:value="${dict['dictValue']}" th:checked="${dict['isDefault'] == 'Y' ? true : false}">
						<label th:for="${dict['dictCode']}" th:text="${dict['dictLabel']}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">宿舍成员：</label>
				<div class="col-sm-8">
					<select id="dormitoryMenber" name="studentId" class="form-control" data-placeholder="实际结果以这里为准！" multiple>
					</select>
					<div class="col-sm-6 pad-zero">
						<select id="selectClass" class="form-control" data-placeholder="请选择班级！">
						</select>
					</div>
					<div class="col-sm-6 pad-zero">
						<select id="selectStudent" class="form-control" data-placeholder="请选择学生！" multiple>
						</select>
					</div>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">
				<div class="form-control-static col-sm-offset-9">
					<button type="submit" class="btn btn-primary">提交</button>
					<button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script th:src="@{/ajax/libs/select/select2.js}"></script>
    <script type="text/javascript">
		$(function(){
			var prefix = ctx + "module/student/dormitory"
			
			var selectGroup = {
				idContainer: [],
				clazz: $("#selectClass"), 	  //选择班级的Select2组件,避免和class重名所以使用clazz
				student: $("#selectStudent"), //选择学生的Select2组件
				menber: $("#dormitoryMenber"),//实际需要提交的结果select
				checkOption: function(id){//校验Option是否以及存在Select选项中
					return this.menber.find("[value='"+id+"']").length > 0;
				},
				getParam: function(e){
					return (e || window.event).params.data;
				}
			}
			
			$("#selectClass").select2({
				ajax: {
			        type:'GET',
			        url: prefix + "/queryClassFormatSelect2",
			        delay: 250,
			        cache: true
			    }
			});
			
			selectGroup.clazz.on("change", function(e) {
				var target = (e || window.event).target;
				var classId = target.value;
				selectGroup.student.empty();
				selectGroup.student.select2({
					ajax: {
				        type:'GET',
				        url: prefix + "/queryStudentFormatSelect2ByClassId",
				        delay: 250,
				        data: function (params) {
				            return {
				            	classId: classId,
				            };
			            },
				        cache: true
				    }
				});
			});
			
			selectGroup.student.on("select2:select", function(e) {
				var params = selectGroup.getParam(e);
				if(selectGroup.checkOption(params.id)){
					selectGroup.idContainer.push(params.id);
					selectGroup.menber.val(selectGroup.idContainer).trigger('change');
				}else{
					//创建新的option
					var newOption = new Option(params.text, params.id, false, true);
					selectGroup.menber.append(newOption).trigger('change');
					selectGroup.idContainer.push(params.id);
				}
			});
			
			selectGroup.menber.on("select2:unselect", function(e){
				var params = selectGroup.getParam(e);
				var index = selectGroup.idContainer.indexOf(params.id,0);
				selectGroup.idContainer.splice(index,1);
			});
			
			$("#form-student-dormitory-add").validate({
				rules:{
					xxxx:{
						required:true,
					},
				},
				submitHandler: function(form) {
					$.operate.save(prefix + "/add", $(form).serialize());
				}
			});
			
		})
	</script>
</body>
</html>
